#include <string>
#include <stdlib.h>
#include "userdata.h"

UserData::UserData(void)
{
	memset(&m_data, 0, sizeof(m_data));
}
UserData& UserData::init(void)
{
	memset(&m_data, 0, sizeof(m_data));
}

UserData& UserData::setTerminal(uint16_t terminal)
{
	m_data.sAddress.usTAddress = terminal;
	return *this;
}
UserData& UserData::setRegion(const string& region)
{
	memcpy(m_data.sAddress.acRegionCode, region.data(), 4);
	return *this;
}
UserData& UserData::setTeam(bool isTeam)
{
	m_data.sAddress.bTeamAddr = isTeam;
	return *this;
}
UserData& UserData::setMac(uint8_t maddress)
{
	m_data.sAddress.ucMAddress = maddress;
	return *this;
}

UserData& UserData::setAfn(eMtAFN afn)
{
	m_data.eAFN = afn;
	return *this;
}

UserData& UserData::setDir(eMtDir dir)
{
	m_data.eDir = dir;
	return *this;
}
UserData& UserData::setPrm(eMtPRM prm)
{
	m_data.ePRM = prm;
	return *this;
}
UserData& UserData::setAcd(bool acd)
{
	m_data.bAcdFcb = acd;
	return *this;
}

UserData& UserData::setTpv(bool tpv)
{
	m_data.bTP = tpv;
	return *this;
}
UserData& UserData::setPos(eMtPos pos)
{
	m_data.ePos = pos;
	return *this;
}
UserData& UserData::setCon(bool con)
{
	m_data.bCon = con;
	return *this;
}
UserData& UserData::setSeq(uint8_t seq)
{
	m_data.ucSeq = seq;
	return *this;
}

//AUX
UserData& UserData::setHavePW(bool havePW)
{
	m_data.bPW = havePW;
	return *this;
}
UserData& UserData::setHaveEC(bool haveEC)
{
	m_data.bEC = haveEC;
	return *this;
}
UserData& UserData::setPw(const uint8_t pw[])
{
	memcpy(m_data.acPW, pw, MT_PW_LEN);
	return *this;
}
UserData& UserData::setEc(uint8_t ec1, uint8_t ec2)
{
	m_data.sEC.ucEC1 = ec1;
	m_data.sEC.ucEC2 = ec2;
	return *this;
}
UserData& UserData::setTp(uint8_t pfc, uint8_t delay, struct tm& t)
{
	m_data.sTP.ucPFC = pfc; 
	m_data.sTP.ucPermitDelayMinutes = delay; 
	m_data.sTP.sDDHHmmss.ucDD = t.tm_mday;
	m_data.sTP.sDDHHmmss.ucHH = t.tm_hour;
	m_data.sTP.sDDHHmmss.ucmm = t.tm_min;
	m_data.sTP.sDDHHmmss.ucss = t.tm_min;
	return *this;
}

//datainfo
UserData& UserData::setNum(uint16_t n)
{
	m_data.usDataNum = n;
	return *this;
}
UserData& UserData::setCmd(eMtCmd cmd, int index)
{
	m_data.sData[index].eCmd = cmd;
	return *this;
}
UserData& UserData::setPn(uint16_t pn, int index)
{
	m_data.sData[index].usPN = pn;
	return *this;
}
UserData& UserData::setApp(bool haveApp, int index)
{
	m_data.sData[index].bApp = haveApp;
	return *this;
}

int UserData::getRegion(void)const
{
	return strtol((const char*)(m_data.sAddress.acRegionCode), 0, 16);
}
string UserData::getRegionString(void)const
{
	return (char*)(m_data.sAddress.acRegionCode);
}
int UserData::getTerminal(void)const
{
	return m_data.sAddress.usTAddress;
}
int UserData::getTeam(void)const
{
	return m_data.sAddress.bTeamAddr;
}
int UserData::getMac(void)const
{
	return m_data.sAddress.ucMAddress;
}

//control
int UserData::getAfn(void)const
{
	return m_data.eAFN;
}
int UserData::getDir(void)const
{
	return m_data.eDir;
}
int UserData::getPrm(void)const
{
	return m_data.ePRM;
}
int UserData::getAcd(void)const
{
	return m_data.bAcdFcb;
}

//sequence
int UserData::getTpv(void)const
{
	return m_data.bTP;
}
int UserData::getPos(void)const
{
	return m_data.ePos;
}
int UserData::getCon(void)const
{
	return m_data.bCon;
}
int UserData::getSeq(void)const
{
	return m_data.ucSeq;
}

//AUX
int UserData::getHavePW(void)const
{
	return m_data.bPW;
}
int UserData::getHaveEC(void)const
{
	return m_data.bEC;
}
const uint8_t* UserData::getPw(void)const
{
	return m_data.acPW;
}
int UserData::getEc(void)const
{
	return 0;
}
int UserData::getTp(void)const
{
	return 0;
}

//userdata
int UserData::getNum(void)const
{
	return m_data.usDataNum;
}
int UserData::getCmd(int index)const
{
	index = index > getNum() ? getNum() : index;
	return m_data.sData[index].eCmd;
}
int UserData::getFn(int index)const
{
	index = index > getNum() ? getNum() : index;
	return m_data.sData[index].eCmd & 0xff;
}
int UserData::getPn(int index)const
{
	index = index > getNum() ? getNum() : index;
	return m_data.sData[index].usPN;
}
int UserData::getApp(int index)const
{
	index = index > getNum() ? getNum() : index;
	return m_data.sData[index].bApp;
}
uMtApp & UserData::getApp(int index)
{
	index = index > getNum() ? getNum() : index;
	return m_data.sData[index].uApp;
};

